package ext.tianma.part.twomaterial;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.ibm.icu.text.NumberFormat;
import ext.tianma.part.forms.CreateNewPartFormProcessor;
import ext.tianma.part.forms.PartUtil;
import ext.tianma.part.util.ClassificationUtil;
import wt.fc.PersistenceHelper;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.util.WTException;
import wt.util.WTProperties;
public class TwoMaterialCommands {
	
	private static String twomaterialExcelPath = "";

	public static WTProperties wtProperties = null;

	private static final String V_DESIGN = "Design";


	static {
		try {
			wtProperties = WTProperties.getLocalProperties();

			twomaterialExcelPath = wtProperties
					.getProperty("ext.tianma.part.twomaterials");
		} catch (Throwable throwable) {
			throw new ExceptionInInitializerError(throwable);
		}
	}
	//生成二级料料号
	public static String generateTwoMaterialNumber(WTObject obj){
		String number = "";
		if (obj != null && obj instanceof WTPart) {
			
			String classificationPath = ClassificationUtil.queryClassificationPath(obj);
			String[] split = classificationPath.split("\\\\");
			String firstClasses = split[1].replace(" ", "");
			//去掉首尾空格
			String secondClasses = split[split.length-1].trim();
			System.out.println("firstClasses:" + firstClasses + ",secondClasses:" + secondClasses);
			number = getTwoMaterialNumber(firstClasses, secondClasses);
			System.out.println("number:" + number);
		}
		return number;
		
	}
	
	
	
	/**
	 * 生成二级料的料号
	 * 
	 * @param sheetName
	 * @param classificationName
	 */
	private static String getTwoMaterialNumber(String sheetName,String classificationName) {
		String number = "";
		String sequence = "";
		try {
			Workbook wb = CreateNewPartFormProcessor.getWorkbook(twomaterialExcelPath);
			if(wb == null ){
				throw new WTException("没有获取到二级料的配置文件，请检查配置文件" + twomaterialExcelPath);
			}
			Sheet sheet = wb.getSheet(sheetName);
			if(sheet == null ){
				throw new WTException("没有获取到二级料的类别，请检查配置文件" + twomaterialExcelPath);
			}
			String shellnamevalue = PartUtil.getTwoMaterialName(sheet, classificationName);
			Sheet sheetSeq = wb.getSheet("seqName");
			if(sheetSeq != null) {
				for (int i = 1; i < sheetSeq.getLastRowNum(); i++) {
					if(sheetName.equals(sheetSeq.getRow(i).getCell(0).getStringCellValue().trim())){
						sequence = sheetSeq.getRow(i).getCell(1).getStringCellValue().trim();
					}
				}
				if("".equals(sheetSeq)){
					throw new WTException("没有获取到二级料的类别的序列，请检查配置文件" + twomaterialExcelPath);
				}
			}
			System.out.println("debug: 获取到的sheetSeq:" + sheetSeq + ",sequence:" + sequence );
			if (shellnamevalue != null) {
				String[] twomaterials = CreateNewPartFormProcessor.splitStr(shellnamevalue);
				String onename = twomaterials[1];
				String twovalue = twomaterials[0];
				String waterCode = getWaterCode(sequence, 6);
				String[] bulNumArray = { onename, twovalue, waterCode };
				number = CreateNewPartFormProcessor.jointString(bulNumArray, "");
			}
		} catch (WTException e) {
			e.printStackTrace();
		}
		return number;
	}
	
	/**
	 * 获取流水码
	 * 
	 * @param seqName
	 * @param codeLengthde
	 * @return String
	 * @throws WTException
	 */
	private static String getWaterCode(String seqName, int codeLength)
			throws WTException {
		String waterCode = PersistenceHelper.manager.getNextSequence(seqName);
		System.out.println("getWatercode,watercode:" + waterCode);
		if (waterCode != null) {
			NumberFormat nf = NumberFormat.getInstance();
			nf.setMinimumIntegerDigits(codeLength);
			nf.setMaximumIntegerDigits(codeLength);
			nf.setGroupingUsed(false);

			waterCode = nf.format(Integer.parseInt(waterCode.trim()));
		}

		if (waterCode == null) {
			waterCode = "";
		}

		return waterCode;
	}
}
